<template>
  <div class="goods-image">
    <!-- 左侧大图-->
    <div
      ref="target"
      class="middle"
      @mouseenter="handleMouseEnter"
      @mousemove="handleMouseMove"
      @mouseleave="handleMouseLeave"
    >
      <img :src="imageList[imgIndex]" alt="" />
      <!-- 蒙层小滑块 -->
      <div
        ref="layer"
        class="layer"
        :style="{ left: `${offsetX}px`, top: `${offsetY}px` }"
      ></div>
    </div>
    <!-- 小图列表 -->
    <ul class="small">
      <li v-for="(img, i) in imageList" :key="i" @mouseenter="imgIndex = i">
        <img :src="img" alt="" />
      </li>
    </ul>
    <!-- 放大镜大图 -->
    <div
      v-show="isLarge"
      :style="[
        {
          backgroundImage: `url(${imageList[imgIndex]})`,
          backgroundPositionX: `${-offsetX * 2}px`,
          backgroundPositionY: `${-offsetY * 2}px`
        }
      ]"
      class="large"
    ></div>
  </div>
</template>

<script>
export default {
  name: '',
  props: {
    imageList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      offsetX: 0,
      offsetY: 0,
      isLarge: false,
      imgIndex: 0
    }
  },
  mounted() {},
  methods: {
    handleMouseEnter() {
      this.isLarge = true
    },
    handleMouseMove($event) {
      const { offsetX, offsetY } = $event
      const { clientWidth: targtWidth, clientHeight: trgetHeight } =
        this.$refs.target
      const { clientWidth: layerWidth, clientHeight: layerHeight } =
        this.$refs.layer
      if (offsetX < layerWidth / 2) {
        this.offsetX = 0
      } else if (targtWidth - (offsetX + layerWidth / 2) <= 0) {
        this.offsetX = targtWidth - layerWidth
      } else {
        this.offsetX = offsetX - layerWidth / 2
      }

      if (offsetY < layerHeight / 2) {
        this.offsetY = 0
      } else if (trgetHeight - (offsetY + layerHeight / 2) <= 0) {
        this.offsetY = trgetHeight - layerHeight
      } else {
        this.offsetY = offsetY - layerHeight / 2
      }
    },
    handleMouseLeave() {
      this.isLarge = false
    }
  }
}
</script>

<style scoped lang="scss">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;

  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    cursor: move;
  }

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    // 背景图:盒子的大小 = 2:1  将来控制背景图的移动来实现放大的效果查看 background-position
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .layer {
    width: 200px;
    height: 200px;
    background: rgba(0, 0, 0, 0.2);
    // 绝对定位 然后跟随咱们鼠标控制left和top属性就可以让滑块移动起来
    left: 0;
    top: 0;
    position: absolute;
    pointer-events: none;
  }

  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      &:hover,
      &.active {
        border: 2px solid $xtxColor;
      }
    }
  }
}
</style>
